package cn.whdreamblog.servers;

import java.text.SimpleDateFormat;
import java.util.List;

import cn.whdreamblog.dao.MessageDAO;
import cn.whdreamblog.daoimp.MessageDAOImp;
import cn.whdreamblog.entites.Message;
import cn.whdreamblog.entites.PageBean;
import cn.whdreamblog.iservers.IMessageServers;

public class MessageServers implements IMessageServers{
	private MessageDAO messageDAO=null;
	private static final int All_DATA = -2;
	public MessageServers() {
		messageDAO=new MessageDAOImp();
	}
	public boolean insertMessage(Message msg){
		return messageDAO.insertMessage(msg);
	};
	public boolean deleteMessage(Message msg){
		return messageDAO.deleteMessage(msg);
	};
	
	public List<Message> getSomeMessages(int start, int limit, int blogId){
		List<Message> msgList = null;
		if(blogId == All_DATA){
			msgList = messageDAO.getAllMessages(start,limit);
		}else{
			msgList = messageDAO.getAllMessages(start,limit,blogId);
		}
		
		SimpleDateFormat sft = new SimpleDateFormat("yyyy年MM月dd日");
		String time;
		if(msgList == null || msgList.isEmpty()){
			return null;
		}
		for (Message message : msgList) {
			time = sft.format(message.getCreatTime());
			message.setFormatTime(time);
			message.setCreatTime(null);
		}
		return msgList; 
	}
	@Override
	public Message getMessageById(Message msg) {
		return messageDAO.getMessageById(msg);
	}
	@Override
	public PageBean getAllMessages2Map(int page, int limit, int blogId) {
		List<Message> msgList = null;
		PageBean p =null;
		if(blogId == All_DATA){
			msgList = messageDAO.getAllMessages2Map(page,limit).getList();
			p = messageDAO.getAllMessages2Map(page,limit);
		}else{
			msgList =messageDAO.getAllMessages2Map(page, limit, blogId).getList();
			p = messageDAO.getAllMessages2Map(page,limit,blogId);
		}
		SimpleDateFormat sft = new SimpleDateFormat("yyyy年MM月dd日");
		String time;
		if(p==null||msgList == null || msgList.isEmpty()){
			return null;
		}
		for (Message message : msgList) {
			time = sft.format(message.getCreatTime());
			message.setFormatTime(time);
			message.setCreatTime(null);
		}
		p.setList(msgList);
		return p;
	}
	
}
